package net.huashitong.Indicators.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.service.RedisService;
import com.sirdc.modules.sys.service.SysScheduleJobService;
import com.sirdc.modules.sys.util.UserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Indicators.dto.BA01DTO;
import net.huashitong.Indicators.dto.StudentDTO;
import net.huashitong.Indicators.entity.AB01;
import net.huashitong.Indicators.entity.BA02;
import net.huashitong.Indicators.filter.BA02Filter;
import net.huashitong.Indicators.service.AB01Service;
import net.huashitong.Indicators.service.AB02Service;
import net.huashitong.Indicators.service.BA02Service;
import net.huashitong.Indicators.service.ManageTimeService;
import net.huashitong.until.Until;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.FontUIResource;
import java.sql.SQLException;
import java.text.ParseException;


@Controller
@RequestMapping("/sys/courseMamage")
public class CourseManageCtrl extends JsonBaseController {


    @Autowired
    private BA02Service datasservice;

    @Autowired
    private AB01Service ab01Service;

    @Autowired
    private AB02Service ab02Service;
    @Autowired
    private RedisService redisService;

    @Autowired
    private ManageTimeService manageTimeService;


    @Override
    protected String getView(String s) {
        return "modules/sys/manage/"+s;
    }
    @RequestMapping(method= RequestMethod.GET)
    public String initPage(Model model)
    {
        String  bab205 = redisService.get(Until.SmesterDate);
        BA02 ba02  = datasservice.getDate(bab205);
        model.addAttribute("ba02",ba02);
        model.addAttribute("bab205Name",Until.ChangeTeam( bab205 ) );
        model.addAttribute("bab205",bab205);
        return getView("query");
    }
    //初始化数据
    @ResponseBody
    @RequestMapping(method= RequestMethod.POST)
    public JqGrid query (@ModelAttribute StudentDTO dto )
    {
        /*String userId = UserUtils.getUserId();
        // List<BA01> ba01s = ba01Service.getAllCourse(filter);
        dto.setAAB998(userId);*/
       // List<StudentDTO> dtos = studentService.getOwerCourse(dto);

        return coverJqGrid(dto, null);
    }

    @RequestMapping(value = "/edit/{id}")
    public String eidtCours(Model model, @PathVariable String  id)
    {

       /* String userId = UserUtils.getUserId();
        //ab01Service.getById()
        //AB01 student  = ab01Service.queryStudent(null,userId);
        BA01 ba01  = ba01Service.getById(id); //获取课程表
        StudentDTO studentDTO  = new StudentDTO();
        studentDTO.setAAB998(userId);
        studentDTO.setBAB001(ba01.getBAB001());
        studentDTO.setBAB003(ba01.getBAB003());


        StudentDTO dto  = ab01Service.getBetting(studentDTO);
        dto.setBAB001(ba01.getBAB001());
        dto.setBAB002(ba01.getBAB002());
        dto.setBAB003(ba01.getBAB003());


        model.addAttribute("course",dto);

        StudentDTO stuDto = new StudentDTO();
        Paging paging = new Paging();
        paging.setCurrentPage(1);
        paging.setPageSize(28);
        stuDto.setPaging(paging);
        stuDto.setBAB001(ba01.getBAB001());
        List<StudentDTO> stulist = ab02Service.getCourseRank(stuDto);
        //model.addAttribute("rankList",stulist);
        model.addAttribute("pageCount",stuDto.getPaging().getPageCount());*/


        return getView("form");
    }

    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Message beattingCourse(@ModelAttribute BA01DTO ba01DTO)
    {

        /*String userid =  SysUserUtils.getUserId();
        AB01 student  = ab01Service.queryStudent(null,userid);

        ba01DTO.setAAB001(student.getAAB001());
        ba01DTO.setAAB998(userid);
        ba01DTO.setBAB003(ba01DTO.getBAB003());
        AB02 betting  = ab02Service.getBetRecord(student.getAAB001(),ba01DTO.getBAB001());

        if(ObjectUtils.isNotBlank(betting))
        {
            try {
                service.updateAb02(betting,ba01DTO.getAAB996(),userid);
            } catch (Exception e) {
                return coverMessage("500","修改失败,余额不足");
            }
        }else {
            try {
                service.updateMemey(ba01DTO.getAAB996(),userid,null);
            } catch (Exception e) {
                return coverMessage("500","投注失败,余额不足");
            }
            service.saveAb02s(ba01DTO);
        }
*/

        return  coverMessage("200");
    }

    @ResponseBody
    @RequestMapping(value = "changedate",method = RequestMethod.POST)
    public Message changDatetime(@RequestBody BA02Filter filter)
    {


        try {
           //datasservice.createOrUpdateDate(filter.getBAB201(),filter.getBAB202(),filter.getBAB203(),filter.getBAB205());
            manageTimeService.createOrUpdateDate(filter.getBAB201(),filter.getBAB202(),filter.getBAB203(),filter.getBAB205());
        } catch (ParseException e) {
            e.printStackTrace();
            return coverMessage("500","更新失败，日期解析失败，请联系管理员");
        } catch (SQLException e) {
            return coverMessage("500","更新余额出错 ，请联系管理员");
        }

        return  coverMessage("200","更新成功");
    }

    @ResponseBody
    @RequestMapping(value = "restart",method = RequestMethod.GET)
    public Message restart()
    {

        String userid  = UserUtils.getUserId();
        if(!"admin".equals(userid))
        {
            return coverMessage("500","非管理员不能操作");
        }

        ab01Service.updateReresue();
        ab02Service.deleteAll();

         redisService.set("choosestatue","1");  //选课状态 1可选，0 不可选
         redisService.set("resultCode"  ,"0");//结果状态  1有结果 0 没有结果
        String courseDate =Until.getHalfYear(Until.getCurrentDate());
        BA02 ba02 = datasservice.getDate(courseDate);
        if(ObjectUtils.isBlank(ba02))
        {
            ba02 = new BA02();
            ba02.setBAB202(Until.getCurrenYear()+"0907");
            ba02.setBAB201(Until.getCurrenYear()+"0901");
            ba02.setBAB203(Until.getCurrenYear()+"0904");
            ba02.setBAB205(courseDate);
            ba02.setBAB204("0");
            datasservice.save(ba02);
            redisService.set("resultCode","0");
        }
        else {
            ba02.setBAB204("0");
            datasservice.update(ba02);
        }


        return coverMessage("200");
    }


    @ResponseBody
    @RequestMapping(value = "getAccount",method = RequestMethod.GET)
    public Message getAccount()
    {

        AB01 ab01 = ab01Service.getAAb999(UserUtils.getUserId());


        return coverMessage("200","ok",ab01.getAAB999());
    }


    @ResponseBody
    @RequestMapping(value = "isOverChoose",method = RequestMethod.POST)
    public Message isOverGame(@RequestBody StudentDTO dto )
    {

        //AB01 ab01 = ab01Service.getAAb999(UserUtils.getUserId());
        BA02 ba02 =  datasservice.getDate(dto.getBAB205());

        if(ObjectUtils.isBlank(ba02))
        {

            return  coverMessage("200");
        }else {
            if("0".equals(ba02.getBAB204()))
            {
                return  coverMessage("200");
            }
        }

        return coverMessage("500","该学期选课已经结束");
    }

}
